package cn.edu.hit.education.model;

import java.util.List;

import cn.edu.hit.education.pojo.College;
import cn.edu.hit.education.pojo.Specialty;
import cn.edu.hit.education.pojo.University;
import cn.edu.hit.education.service.ICollegeService;
import cn.edu.hit.education.service.ISpecialtyService;
import cn.edu.hit.education.service.IUniversityService;
import educationschedule.Application;

public class DepartmentRootNode extends Node {
	IUniversityService universityService = (IUniversityService)Application.applicationContext.getBean("universityServiceImpl");
	ICollegeService collegeService = (ICollegeService)Application.applicationContext.getBean("collegeServiceImpl");
	ISpecialtyService specialtyService = (ISpecialtyService)Application.applicationContext.getBean("specialtyServiceImpl");
	
	private static DepartmentRootNode root = null;

	private DepartmentRootNode() {
	}

	public static synchronized DepartmentRootNode getRootNode() {
		//if (root == null) 
		{
			root = new DepartmentRootNode();
			root.setParent(null);
			root.initRootNode();
		}
		return root;
	}

	private void initRootNode() {
		
		OrganizationNode organizationNode = new OrganizationNode(0,"组织结构");
		root.addChild(organizationNode);
		
		List<University> universities = universityService.queryAllUniversity();
		
		if(universities != null && universities.size() > 0){
			for (University university : universities) {
				UniversityNode universityNode = new UniversityNode(university.getId(),university.getName());
				organizationNode.addChild(universityNode);
				
				List<College> colleges = collegeService.queryCollegesByUniversityId(university.getId());
				if(colleges != null && colleges.size() > 0){
					for (College college : colleges) {
						CollegeNode collegeNode = new CollegeNode(college.getId(),college.getName());
						universityNode.addChild(collegeNode);
						
						List<Specialty> specialties = specialtyService.querySpecialtiesByCollegeId(college.getId());
						if(specialties != null && specialties.size() > 0){
							for (Specialty specialty : specialties) {
								SpecialtyNode specialtyNode = new SpecialtyNode(specialty.getId(),specialty.getName());
								collegeNode.addChild(specialtyNode);
							}
						}
					}
				}
			}
		}
	}
}
